package com.D5Power.basic
{
	import com.D5Power.loader.MutiLoader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class GraphicsData
	{
		/**
		 * 裸模
		 */ 
		private var _body:String;
		
		/**
		 * 装扮列表
		 */ 
		private var _avatarName:Array;
		
		/**
		 * 每行最大帧数的配置
		 */ 
		private var _lineFrame:Array;
		
		/**
		 * 帧 数 动画的宽
		 */ 
		private var _framesTotal:uint = 1;
		
		/**
		 * 实际现实的单元宽度
		 */ 
		private var _frameWidth:uint=0;
		
		/**
		 * 实际现实的单元高度
		 */ 
		private var _frameHeight:uint=0;
		
		/**
		 * 速度
		 */ 
		private var _fps:Number = 0;
		
		/**
		 * 动画的层(高)数，对于角色就是方向/动作
		 */ 
		private var _framesLayer:int=1;
		
		/**
		 * 是否需要镜像
		 */ 
		private var _needMirror:Boolean;
		
		/**
		 * 非使用资源池类的资源
		 */ 
		private var _bitmap:BitmapData;
		
		/**
		 * 是否使用资源池
		 */ 
		private var _usePool:Boolean;
		
		private var _isReady:Boolean=false;
		
		private var _fillList:Array;
		
		private var _fillMirrorList:Array;
		
		public function GraphicsData(body:String='',framesTotal:int = 1,framesLayer:int=1,fps:Number = 0,needMirror:Boolean=true)
		{
			_body = body;
			_framesTotal = framesTotal;
			_framesLayer = framesLayer;
			_fps = fps;
			_needMirror = needMirror;
			
			_fillList = new Array();
			_fillMirrorList = new Array();
			
			createBody();
		}
		public function get Ready():Boolean
		{
			return _isReady;
		}
		/**
		 * 设置资源
		 */ 
		public function set bitmap(v:BitmapData):void
		{
			_bitmap = v;
			
			updateFrameConfig(_bitmap);
			updateFillList();
		}
		
		public function get bitmap():BitmapData
		{
			if(_body=='') return _bitmap;

			return Global.resPool.getResource(_body);
		}
		
		public function get needMirror():Boolean
		{
			return _needMirror;
		}
		
		/**
		 * 单帧宽度
		 */ 
		public function get frameWidth():uint
		{
			return _frameWidth;
		}
		
		/**
		 * 单帧高度
		 */ 
		public function get frameHeight():uint
		{
			return _frameHeight;
		}
		
		/**
		 * 横向总帧数
		 */ 
		public function get framesTotal():uint
		{
			return _framesTotal;
		}
		/**
		 * 纵向总帧数
		 */ 
		public function get framesLayer():uint
		{
			return _framesLayer;
		}
		/**
		 * 渲染频率
		 */ 
		public function get fps():uint
		{
			return _fps;
		}
		
		private function createBody():void
		{
			if(_body=='') return;
			if(Global.resPool.getResource(_body)==null) Global.getCharacterRes(_body);
			
			var bitmap:BitmapData = Global.resPool.getResource(_body);
			
			updateFrameConfig(bitmap);
			updateFillList();
			
			if(_needMirror) createMirror(_body);

		}
		
		private function createMirror(name:String):void
		{
			if(Global.resPool.getResource(name+'_mirror')!=null) return;
			
			var bitmap:BitmapData = Global.resPool.getResource(name);
			var matrix:Matrix=new Matrix(-1,0,0,1,bitmap.width);
			
			var bitmap2:BitmapData = new BitmapData(bitmap.width, bitmap.height,true,0x00000000);
			bitmap2.draw(bitmap,matrix, null);
			
			Global.resPool.addResource(name+'_mirror',bitmap2)
		}
		
		/**
		 * 直接通过URL地址数组来增加图形资源
		 * @param	list	包含URL地址的资源数组
		 */ 
		public function addLoadResource(list:Array,resName:Array):void
		{
			// 先检索资源池，看是否存在同名资源
			var loadList:Array = new Array();
			var loadName:Array = new Array();
			var length:uint = resName.length;
			for(var i:uint = 0; i<length; i++)
			{
				// 只有资源池中不存在的资源，才会纳入加载地址
				if(Global.resPool.getResource(resName[i])==null)
				{
					loadName.push(resName[i]);
					loadList.push(list[i]);
				}
			}
			var mloader:MutiLoader = new MutiLoader(new Array());
			mloader.takeData = loadName;
			mloader.addEventListener(Event.COMPLETE,onResourceLoaded);
			mloader.loadDoc(loadList);
			
			_avatarName = resName;
		}
		
		private function onResourceLoaded(e:Event):void
		{
			var target:MutiLoader = e.target as MutiLoader;
			// 加载完成后，依次推入资源库
			var length:uint = target.takeData.length;
			
			for(var i:uint=0; i<length; i++)
			{
				if(Global.resPool.getResource(target.takeData[i])!=null) continue; // 已存在同名资源
				
				var bitmap:BitmapData = new BitmapData(target.Data[i].width,target.Data[i].height,true,0x00000000);
				bitmap.draw(target.Data[i]);
				Global.resPool.addResource(target.takeData[i],bitmap);
				
				
				if(_needMirror) createMirror(target.takeData[i]);
				
				if(_body=='' && i==0) updateFrameConfig(bitmap);
			}
			
			target.clear();
			updateFillList();
		}
		
		private function updateFrameConfig(bitmap:BitmapData):void
		{
			_frameWidth=bitmap.width/_framesTotal;
			_frameHeight=bitmap.height/_framesLayer;
			
		}
		
		private function updateFillList():void
		{
			_fillList.splice(0,_fillList.length);
			_fillMirrorList.splice(0,_fillMirrorList.length);
			
			if(Global.resPool.getResource(_body)!=null)
			{
				_fillList.push(Global.resPool.getResource(_body));
				_fillMirrorList.push(Global.resPool.getResource(_body+'_mirror'));
			}
			
			for each(var resName:String in _avatarName)
			{
				_fillList.push(Global.resPool.getResource(resName));
				_fillMirrorList.push(Global.resPool.getResource(resName+'_mirror'));
			}
			
			_isReady = true;
		}
		
		/**
		 * 填充数据
		 * @param		buffer		缓冲区
		 * @param		rect		矩形
		 * @param		p			填充目标
		 * @param		isMirror	镜像扩展，如果需要复制镜像文件，请设置为_mirror
		 */ 
		public function fill(buffer:BitmapData,rect:Rectangle,p:Point):void
		{
//			var bitmap:BitmapData;
//			if(Global.resPool.getResource(_body+isMirror)!=null)
//			{
//				bitmap = Global.resPool.getResource(_body+isMirror);
//				buffer.copyPixels(bitmap,rect,p,null,null,true);
//			}
//			
//			for each(var resName:String in _avatarName)
//			{
//				bitmap = Global.resPool.getResource(resName+isMirror);
//				if(bitmap==null) continue;
//				buffer.copyPixels(bitmap,rect,p,null,null,true);
//			}
			for each(var source:BitmapData in _fillList)
			{
				buffer.copyPixels(source,rect,p,null,null,true);
			}
			
		}
		
		public function fillMirror(buffer:BitmapData,rect:Rectangle,p:Point):void
		{
			for each(var source:BitmapData in _fillMirrorList)
			{
				buffer.copyPixels(source,rect,p,null,null,true);
			}
		}
		
		public function clear():void
		{
			
		}
		
		/**
		 * 获取当前动作行的最大帧数
		 */ 
		public function getFrameTotal(line:uint):uint
		{
			if(_lineFrame==null || _lineFrame[line]==null)
				return _framesTotal;
			else
				return _lineFrame[line];
		}
		
		/**
		 * 获取开始帧数，专门用于镜像计算
		 */ 
		public function getStartFrame(line:uint):uint
		{
			if(_lineFrame==null || _lineFrame[line]==null)
				return 0;		
			else
				return _framesTotal-_lineFrame[line];
		}
		
		/**
		 * 设置各行最大帧数
		 */ 
		protected function set lineFrame(arr:Array):void
		{
			_lineFrame = new Array();
			// 将数据复制过来
			for(var i:uint = 0;i<arr.length;i++)
			{
				_lineFrame.push(arr[i]);
			}
		}
	}
}